from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAdminUser, DjangoModelPermissions
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SKU, SPU, SPUSpecification
from apps.meiduo_admin.serializers.sku import SKUSerializer, GoodsCategorySerializer, SimpleSPUSerializer, \
    SPUSpecModelSerializer
from apps.meiduo_admin.utils import PageNum


class SKUModelViewSet(ModelViewSet):
    # 懒加载
    queryset = SKU.objects.all()

    serializer_class = SKUSerializer
    # 添加分页
    pagination_class = PageNum
    # 额外添加 DjangoModelPermissions
    permission_classes = [IsAdminUser, DjangoModelPermissions]


# class SKUModelViewSet(ModelViewSet):
#
#     queryset = SKU.objects.all()
#     serializer_class = SKUSerializer
#     # 添加分页
#     pagination_class = PageNum


# 查询3级分类
class ThreeCategoryAPIView(APIView):
    def get(self, request):
        # 获取查询结果集
        cats = GoodsCategorySerializer(subs=None)
        # 创建序列化器,进行转换
        serializer = GoodsCategorySerializer(cats, many=True)
        # 返回
        return Response(serializer.data)


# class ThreeCategoryAPIView(APIView):
#     def get(self, request):
#         cats = GoodsCategorySerializer(subs=None)
#         serializer = GoodsCategorySerializer(cats, many=True)
#         return Response(serializer.data)


# 获取所有的SPU信息
class SPUListAPIView(ListAPIView):

    queryset = SPU.objects.all()

    serializer_class = SimpleSPUSerializer


# class SPUListAPIView(ListAPIView):
#     queryset = SPU.objetcs.all()
#     serializer_class = SimpleSPUSerializer


# 根据SPU来获取规格和规格选项
class SPUSpecsAPIView(APIView):

    def get(self, request, pk):
        # pk = spu_id
        # 根据spu_id 过滤筛选获取 SPU规格
        specs = SPUSpecification.objects.filter(spu_id=pk)
        # 定义序列化器,将对象列表转换为字典列表
        serializer = SPUSpecModelSerializer(specs, many=True)
        # 返回
        return Response(serializer.data)


# class SPUSpecsAPIView(APIView):
#     def get(self, request, pk):
#         # 根据spu_id过滤筛选获取SPU规格
#         specs = SPUSpecification.objects.filter(spu_id=pk)
#         # 定义序列化器,将对象列表转换为字典列表
#         serializer =SPUSpecModelSerializer(specs, many=True)
#         # 返回
#         return Response(serializer.data)
